package com.flow.service.impl;

import java.util.List;

import com.flow.dao.INumberIndexDao;
import com.flow.model.NumberIndex;
import com.flow.service.INumberIndexService;
import com.flow.tools.StringTools;

public class NumberIndexService<T> implements INumberIndexService<T> {

	private INumberIndexDao<T> niDao;
	
	@Override
	public boolean insertNumberIndex(NumberIndex ni) {
		
		if(null == ni)
			return false;
		
		if(!StringTools.nullOrEmpty(ni.getNiCode()))
			return false;
		
		if(!StringTools.nullOrEmpty(ni.getNiNumber()))
			return false;
		
		if(null == ni.getNiStatus())
			return false;
		
		try {
			
			if(niDao.insertNumberIndex(ni))
				return true;
			else
				return false;
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return false;
	}

	@Override
	public String getNumberIndex() {

		String number = "000000";
		
		try {
			
			String hql = "from NumberIndex order by niId desc";
			List<T> nfList = niDao.findNumberIndexAllByHql(hql);
			
			if(null == nfList || nfList.isEmpty())
				return number;
			
			NumberIndex nf = (NumberIndex)nfList.get(0);
			return nf.getNiNumber();
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return null;
	}

	public void setNiDao(INumberIndexDao<T> niDao) {
		this.niDao = niDao;
	}

}
